package seedpod.kb2db.protegeplugin;
/**
 * Copyright 2009 University of  Washington
 * This code is released under the GPL version 3 - with a copy of the license included
 * @author Maya Hao Li
 *
 */
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import seedpod.kb2db.ProjectTransformValidator;
import seedpod.kb2db.Protege2Rdb;
import edu.stanford.smi.protege.model.Project;
import edu.stanford.smi.protege.storage.clips.ClipsKnowledgeBaseFactory;
import edu.stanford.smi.protege.util.PropertyList;
import edu.stanford.smi.protege.util.URIUtilities;

public class ProtegeProjectCopier
{

	private static String NEW_PROJECT_PREFIX = "copy_";
	private static String DATE_FORMAT = "yyyyMMdd_hhmmss";
	private static String EXTENSION = Protege2Rdb.VALID_EXTENSION;
	private Project _project;
	private URI _sourceURI;
	private URI _targetURI;

	public ProtegeProjectCopier(Project project) {
		_project = project;
		_sourceURI = _project.getProjectURI();
		ProjectTransformValidator.isProjectFilePprj( _project );
	}

	// creates a default name copy_[projectName]_CurrentDate
	public Project copyProject() {
		String newProjectName = _project.getName();
		if (newProjectName != null )
			newProjectName = createDefaultNewName( newProjectName );
		else 
			newProjectName = createDefaultNewName("seedpod");
		return copyProject( newProjectName );
	}

	// resave the project in the newName (old project is not deleted).
	// It's saved in the original project URI path. newName can be null
	public Project copyProject( String newProjectName ) {
		URI pUri = _project.getProjectDirectoryURI();
		if ( pUri == null )
			_targetURI = URIUtilities.createURI( newProjectName );
		else
			_targetURI = createNewUri( _project.getProjectURI(), newProjectName );

		copyProject( _targetURI );
		return _project;
	}

	public Project copyProject( URI newUri ) {
		_targetURI = URIUtilities.ensureExtension( newUri, EXTENSION );
		_project.setProjectURI( _targetURI );
		_project.getKnowledgeBase().setName( _project.getName() );

		/**
		 * HACK make copies of the .pont and .pins files of the knowledge base
		 * and rename them with the new URI
		 */
		PropertyList sources = _project.getSources();
		ClipsKnowledgeBaseFactory.setSourceFiles( sources, null, null );
		_project.getKnowledgeBaseFactory().saveKnowledgeBase( _project.getKnowledgeBase(), sources, new ArrayList<String>() );
		Protege2Rdb.saveProject( _project );

		return _project;
	} 
	
	public Project copyProject (URI newUri, boolean save) {
		_targetURI = URIUtilities.ensureExtension( newUri, EXTENSION );
		_project.setProjectURI( _targetURI );
		_project.getKnowledgeBase().setName( _project.getName() );

		/**
		 * HACK make copies of the .pont and .pins files of the knowledge base
		 * and rename them with the new URI
		 */
		PropertyList sources = _project.getSources();
		ClipsKnowledgeBaseFactory.setSourceFiles( sources, null, null );
		_project.getKnowledgeBaseFactory().saveKnowledgeBase( _project.getKnowledgeBase(), sources, new ArrayList<String>() );

		return _project;
		
	}

	private void sources( Project p ) {
		System.out.println( p.getProjectURI() );

		PropertyList sources = p.getSources();
		Iterator<?> nIter = sources.getNames().iterator();
		while (nIter.hasNext()) {
			String key = (String) nIter.next();
			String val = sources.getString( key );
			System.out.println( key + "= " + val );
		}
	}

	// create new URI using old path and new name
	protected URI createNewUri( URI oldUri, String newProjectName ) {
		URI newUri;
		URI directory = URIUtilities.getParentURI( oldUri );
		newUri = URIUtilities.createURI( directory + "/" + newProjectName );
		newUri = URIUtilities.ensureExtension( newUri, EXTENSION );
		return newUri;
	}

	private String createDefaultNewName( String pName ) {
		return NEW_PROJECT_PREFIX + pName + "_" + getCurrentTimeStamp();
	}

	private static String getCurrentTimeStamp() {
		return getCurrentTimeStamp( DATE_FORMAT );
	}
	

	public static String getCurrentTimeStamp( String pattern) {
		if (pattern == null ) 
			pattern = "yyyyMMddhhmmss";
		SimpleDateFormat dateFormat = new SimpleDateFormat( pattern );
		Date now = new Date();
		return dateFormat.format( now );
	}

	public URI getSourceProjectURI() {
		return _sourceURI;
	}

	public URI getTargetProjectURI() {
		return _targetURI;
	}

	public static void main( String[] args ) {
		Project testProj = Project.loadProjectFromFile( "KB/testModel/test.pprj", new ArrayList<String>() );
		ProtegeProjectCopier copier = new ProtegeProjectCopier( testProj );
		copier.copyProject( URIUtilities.createURI( "testProject" ) );
		copier.sources( copier._project );
	}
}
